package com.ll.action;

import java.sql.Connection;
import java.sql.PreparedStatement;

import com.lee.model.ManagerUserBean;
import com.lee.model.ServiceUserBean;
import com.lee.sender.DB;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class ServiceRegAction extends ActionSupport implements ModelDriven<ServiceUserBean>
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -1507953853909629335L;
	private ServiceUserBean u = new ServiceUserBean();
	
	public ServiceUserBean getU()
	{
		return u;
	}
	public void setU(ServiceUserBean u)
	{
		this.u = u;
	}
	
	public ServiceUserBean getModel()
	{
		return u;
	}
	
	public String execute() throws Exception
	{
		// 开始写数据库
		Connection con = null;
		try
		{
			//获取登录的管理员账户
			ManagerUserBean mUser = (ManagerUserBean)
					ActionContext.getContext().getSession().get("_manager_user");
			
			if(mUser == null)
			{
				return ERROR;
			}
			
			//插入客服账户到数据库
			con = DB.getConnection();
			PreparedStatement st = 
					con.prepareStatement("INSERT INTO t_service_user(username, password," +
							"dept, name, officePhone, sex, " +
							"email, mobilephone, qq, msn, managerId)" +
							" VALUES(?,?,?,?,?,?,?,?,?,?,?)");
			st.setString(1, u.getUsername());
			st.setString(2, u.getPassword());
			st.setString(3, u.getDept());
			st.setString(4, u.getName());
			st.setString(5, u.getOfficePhone());
			st.setString(6, u.getSex());
			st.setString(7, u.getEmail());
			st.setString(8, u.getMobilephone());
			st.setString(9, u.getQq());
			st.setString(10, u.getMsn());
			st.setString(11, mUser.getUsername());
			
			int re = st.executeUpdate();
			
			if(re == 1)
			{
				ActionContext.getContext().put("AddServiceUser", "true");
				
				return SUCCESS;
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			try {
				if(con != null)
					con.close();
				}catch(Exception e){
					e.printStackTrace();
				}
		}
		return ERROR;	
	}
}
